<!-- src/pagesUser/declaration/declaration.vue -->
<template>
  <view class="viewport">
    <!-- tabs -->
    <view class="tabs">
      <text class="item" 
        v-for="(item, index) in activityTabs"
        :key="item.title"
        @tap="activeIndex = index"
      >{{ item.title }}</text>
      
      <!-- 游标 -->
      <view class="cursor" :style="{ left: activeIndex * 50 + '%' }"></view>
    </view>
    
    <!-- 滑动容器 -->
    <swiper class="swiper" :current="activeIndex" @change="activeIndex = $event.detail.current">
      <!-- 我申报的活动 -->
      <swiper-item>
        <scroll-view scroll-y class="activity-scroll">
          <view class="activity-list">
            <view class="activity-item" v-for="item in createdActivities" :key="item.activityId" @tap="goToActivityDetail(item.activityId)">
              <view class="activity-image">
                <image :src="item.activityImage" mode="aspectFill" />
              </view>
              <view class="activity-info">
                <view class="title">{{ item.title }}</view>
                <view class="description">{{ item.description }}</view>
                <view class="time">时间：{{ formatDate(item.startTime) }} - {{ formatDate(item.endTime) }}</view>
                <view class="location">地点：{{ item.location }}</view>
                <view class="status">
                  <text :class="getStatusClass(item.status)" class="status-tag">
                    {{ getStatusText(item.status) }}
                  </text>
                </view>
              </view>
            </view>
            
            <!-- 空状态 -->
            <view v-if="createdActivities.length === 0" class="empty">
              <text>暂无申报的活动</text>
            </view>
          </view>
        </scroll-view>
      </swiper-item>
      
      <!-- 我参加的活动 -->
      <swiper-item>
        <scroll-view scroll-y class="activity-scroll">
          <view class="activity-list">
            <view class="activity-item" v-for="item in participatedActivities" :key="item.activityId" @tap="goToActivityDetail(item.activityId)">
              <view class="activity-image">
                <image :src="item.activityImage" mode="aspectFill" />
              </view>
              <view class="activity-info">
                <view class="title">{{ item.title }}</view>
                <view class="description">{{ item.description }}</view>
                <view class="time">时间：{{ formatDate(item.startTime) }} - {{ formatDate(item.endTime) }}</view>
                <view class="location">地点：{{ item.location }}</view>
                <view class="status">
                  <text :class="getParticipationStatusClass(item.participationStatus)" class="status-tag">
                    {{ getParticipationStatusText(item.participationStatus) }}
                  </text>
                </view>
              </view>
            </view>
            
            <!-- 空状态 -->
            <view v-if="participatedActivities.length === 0" class="empty">
              <text>暂无参加的活动</text>
            </view>
          </view>
        </scroll-view>
      </swiper-item>
    </swiper>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getUserActivityAPI, getUserActivityDetailAPI } from '../../services/voluntary.js'
// 导入 uni.navigateTo 用于页面跳转
import { onLoad } from '@dcloudio/uni-app'

// tabs 数据
const activityTabs = ref([
  { title: '我申报的' },
  { title: '我参加的' }
])

// 高亮显示索引
const activeIndex = ref(0)

// 我申报的活动列表
const createdActivities = ref([])

// 我参加的活动列表
const participatedActivities = ref([])

// 获取我申报的活动
const loadCreatedActivities = async () => {
  try {
    const res = await getUserActivityAPI()
    if (res.data && res.code === 1) {
      createdActivities.value = res.data || []
    }
  } catch (error) {
    console.error('获取申报活动失败:', error)
  }
}

// 获取我参加的活动
const loadParticipatedActivities = async () => {
  try {
    const res = await getUserActivityDetailAPI()
    if (res.data && res.code === 1) {
      participatedActivities.value = res.data || []
    }
  } catch (error) {
    console.error('获取参与活动失败:', error)
  }
}

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`
}

// 获取状态文本（我申报的活动）
const getStatusText = (status) => {
  const statusMap = {
    1: '招募中',
    2: '进行中',
    3: '已结束',
    4: '已取消'
  }
  return statusMap[status] || '未知状态'
}

// 获取状态样式类（我申报的活动）
const getStatusClass = (status) => {
  const classMap = {
    1: 'status-pending',
    2: 'status-active',
    3: 'status-finished',
    4: 'status-cancelled'
  }
  return classMap[status] || ''
}

// 获取参与状态文本（我参加的活动）
const getParticipationStatusText = (status) => {
  const statusMap = {
    0: '已报名',
    1: '已签到',
    2: '已签退',
    3: '已取消'
  }
  return statusMap[status] || '未知状态'
}

// 获取参与状态样式类（我参加的活动）
const getParticipationStatusClass = (status) => {
  const classMap = {
    0: 'status-signed',
    1: 'status-checked-in',
    2: 'status-checked-out',
    3: 'status-cancelled'
  }
  return classMap[status] || ''
}

// 跳转到活动详情页面
const goToActivityDetail = (activityId) => {
  uni.navigateTo({
    url: `/pages/voluntary/voluntaryDetail/voluntaryDetail?id=${activityId}`
  })
}

// 初始化加载数据
onMounted(() => {
  loadCreatedActivities()
  loadParticipatedActivities()
})
</script>

<style lang="scss">
page {
  height: 100%;
  overflow: hidden;
}

.viewport {
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: #fff;
}

// tabs
.tabs {
  flex-shrink: 0;
  display: flex;
  justify-content: space-around;
  line-height: 60rpx;
  margin: 0 10rpx;
  background-color: #fff;
  box-shadow: 0 4rpx 6rpx rgba(240, 240, 240, 0.6);
  position: relative;
  z-index: 9;

  .item {
    flex: 1;
    text-align: center;
    padding: 20rpx;
    font-size: 28rpx;
    color: #262626;
  }

  .cursor {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 50%;
    height: 6rpx;
    padding: 0;
    background-color: #27ba9b;
    /* 过渡效果 */
    transition: all 0.4s;
  }
}

// swiper
.swiper {
  flex: 1;
  background-color: #f7f7f8;
}

.activity-scroll {
  height: 100%;
}

.activity-list {
  padding: 20rpx;
}

.activity-item {
  display: flex;
  padding: 20rpx;
  background-color: #fff;
  border-radius: 10rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
}

.activity-image {
  width: 180rpx;
  height: 180rpx;
  border-radius: 10rpx;
  overflow: hidden;
  margin-right: 20rpx;
  
  image {
    width: 100%;
    height: 100%;
  }
}

.activity-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 10rpx;
}

.description {
  font-size: 26rpx;
  color: #666;
  margin-bottom: 10rpx;
  flex: 1;
}

.time, .location {
  font-size: 24rpx;
  color: #999;
  margin-bottom: 5rpx;
}

.status {
  margin-top: 10rpx;
}

.status-tag {
  padding: 4rpx 12rpx;
  border-radius: 4rpx;
  font-size: 24rpx;
  color: #fff;
}

.status-pending, .status-signed {
  background-color: #fa8c16;
}

.status-active, .status-checked-in {
  background-color: #52c41a;
}

.status-finished, .status-checked-out {
  background-color: #1890ff;
}

.status-cancelled {
  background-color: #f5222d;
}

.empty {
  text-align: center;
  padding: 40rpx;
  color: #999;
}
</style>